package com.dodock.banglapapers.server.helper;

import java.io.IOException;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class DDNNewsListSamakal implements DDNNewsListInterface {

	private String BASE_URL = "http://www.samakal.net/";

	@Override
	public String GenerateNewsList(String categoryid) {
		try {
			String url = BASE_URL;
			if (!categoryid.equalsIgnoreCase("latest")) {
				url = url + categoryid;
			}
			return generateNewsListFromXmlFeed(url, categoryid);
		} catch (Exception ex) {
			System.out.print("exeception : " + ex.toString());
			return "[]";
		}
	}

	private String generateNewsListFromXmlFeed(String url, String categoryid)
			throws IOException {

		if (categoryid.equals("latest")) {
			return getLatestNewsList(url);
		} else {
			return getNewsList(url);
		}
	}

	private void parseNewsObject(JsonArray array, Elements links){
		JsonObject obj;
		for (Element aLink : links) {
			obj = new JsonObject();
			String aUrl = aLink.attr("href").trim();
//			String id = aUrl.substring(aUrl.lastIndexOf("net/") + 5);
			String id = aUrl;
			String img = "";
			Element imageElement = aLink.select("img").first();
			img = imageElement.attr("data-original");
			Element newsDtlElement = aLink
					.select("div[id=hl2]").first();
			String text = newsDtlElement.text();
			obj.addProperty("id", id);
			obj.addProperty("title", text);
			obj.addProperty("image", img);
			array.add(obj);
		}
	}
	
	private String getNewsList(String url) {
		// TODO Auto-generated method stub
		JsonArray array = new JsonArray();
		Document doc;
		try {
			//doc = Jsoup.connect(url).get();
			Connection.Response response = Jsoup.connect(url)
		            .userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
		            .timeout(10000)
		            .execute();
			
			doc = response.parse();
			int i = 1;
			String baseCatLeadNews = "cat_lead_news_";
			String catLeadNews = baseCatLeadNews + i;
			Elements mastHeads = null;
			mastHeads = doc.select("div[id=" + catLeadNews + "]");
			while (!mastHeads.isEmpty()) {
				Elements links = mastHeads.select("a[href]");
				parseNewsObject(array, links);
				i++;
				catLeadNews = baseCatLeadNews + i;
				mastHeads = null;
				mastHeads = doc.select("div[id=" + catLeadNews + "]");
			}
			
			catLeadNews = "cat_more_news";
			mastHeads = null;
			mastHeads = doc.select("div[id=" + catLeadNews + "]");
			for(Element head: mastHeads){
				Elements links = head.select("a[href]");
				parseNewsObject(array, links);
			}
			
			catLeadNews = "cat_lead_img_hl";
			mastHeads = null;
			mastHeads = doc.select("div[id=" + catLeadNews + "]");
			for(Element head: mastHeads){
				Elements links = head.select("a[href]");
				parseNewsObject(array, links);
			}
			
			return array.toString();
		} catch (Exception e) {
			System.out.println(e.toString());
			return "[]";
		}
	}

	private String getLatestNewsList(String url) {
		// TODO Auto-generated method stub
		JsonArray array = new JsonArray();
		JsonObject obj;

		Document doc;
		try {
			//doc = Jsoup.connect(url).get();
			Connection.Response response = Jsoup.connect(url)
		            .userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
		            .timeout(10000)
		            .execute();
			
			doc = response.parse();

			Elements mastHeads = doc.select("div[id=latest_view_content]");
			Element head = mastHeads.first();
			mastHeads = head.select("a[href]");
			for (Element aElement : mastHeads) {
				obj = new JsonObject();
				String aUrl = aElement.attr("href").trim();
//				String id = aUrl.substring(aUrl.lastIndexOf("net/") + 5);
				String id = aUrl;
				obj.addProperty("id", id);
				obj.addProperty("title", aElement.text());
				array.add(obj);
			}
			return array.toString();
		} catch (IOException e) {
			return "[]";
		}
	}
}
